#
# SConscript build file for the One Button. 
# Called automatically by the SConstruct in the parent directory
#

import os

# import everything exported in SConstruct

Import('*')

# get a copy of the environment

env=env.Clone()

mainfile = "onebutton"

# verify that this example is compatible with the selected MCU

sconscriptFile=(lambda x:x).func_code.co_filename
sconscriptDir=os.path.dirname(sconscriptFile)
compatFile=os.path.join(sconscriptDir,"compat.txt");

supported=True
if os.path.exists(compatFile):
  supported=False
  for line in open(compatFile,"r"):
    line=line.rstrip()
    if not line.startswith("#") and line==mcu:
      supported=True
      break

if supported:

  # get all the sources

  matches=[]
  matches.append(Glob("*.cpp"))
  matches.append(Glob("*.c"))
  matches.append(Glob("*.h"))
  matches.append(Glob("*.asm"))
  matches.append("system/LibraryHacks.cpp")

  # handle MCU differences

  if mcu.startswith("f4"):
    matches.append("system/f407_168_8/Startup.asm")
    matches.append("system/f407_168_8/System.c")
    linkerscript="src/system/f407_168_8/Linker.ld"

  elif mcu=="f1hd":
    matches.append("system/f1hd_72_8/Startup.asm")
    matches.append("system/f1hd_72_8/System.c")
    linkerscript="src/system/f1hd_72_8/Linker.ld"

  elif mcu=="f1cle":
    matches.append("system/f107_72_8/Startup.asm")
    matches.append("system/f107_72_8/System.c")
    linkerscript="src/system/f107_72_8/Linker.ld"

  elif mcu=="f1md":
    matches.append("system/f1md/Startup.asm")
    matches.append("system/f1md/System.c")
    linkerscript="src/system/f1md/Linker.ld"

  elif mcu=="f1mdvl":
    matches.append("system/f1mdvl_24_8/Startup.asm")
    matches.append("system/f1mdvl_24_8/System.c")
    linkerscript="src/system/f1mdvl_24_8/Linker.ld"

  elif mcu=="f042":
    matches.append("system/f042_48_8/Startup.asm")
    matches.append("system/f042_48_8/System.c")
    linkerscript="src/system/f042_48_8/Linker.ld"

  elif mcu=="f051":
    matches.append("system/f051_48_8/Startup.asm")
    matches.append("system/f051_48_8/System.c")
    linkerscript="src/system/f051_48_8/Linker.ld"

  elif mcu=="f030":
    matches.append("system/f030_48_8/Startup.asm")
    matches.append("system/f030_48_8/System.c")
    linkerscript="src/system/f030_48_8/Linker.ld"

  buildoutdir="src/build/"+systemprefix

  # set the additional linker flags

  env.Append(LINKFLAGS=["-T"+linkerscript,"-Wl,-wrap,__aeabi_unwind_cpp_pr0","-Wl,-wrap,__aeabi_unwind_cpp_pr1","-Wl,-wrap,__aeabi_unwind_cpp_pr2","-Wl,-wrap,atexit"])

  # additional include directory for the graphics

  env.Append(ASFLAGS="-Isrc")

  # unique additions for this example

  env.Append(CPPPATH="#src")

  # trigger a build with the correct library name

  elf=env.Program(mainfile+".elf",matches)
  hex=env.Command(mainfile+".hex",elf,"arm-none-eabi-objcopy -O ihex "+buildoutdir+"/"+mainfile+".elf "+buildoutdir+"/"+mainfile+".hex")
  bin=env.Command(mainfile+".bin",elf,"arm-none-eabi-objcopy -O binary "+buildoutdir+"/"+mainfile+".elf "+buildoutdir+"/"+mainfile+".bin")
  lst=env.Command(mainfile+".lst",elf,"arm-none-eabi-objdump -h -S "+buildoutdir+"/"+mainfile+".elf > "+buildoutdir+"/"+mainfile+".lst")
  size=env.Command(mainfile+".size",elf,"arm-none-eabi-size --format=berkeley "+buildoutdir+"/"+mainfile+".elf | tee "+buildoutdir+"/"+mainfile+".size")

  # install the library if the user gave the install option

  #EXAMPLEINSTALLDIR=INSTALLDIR+buildoutdir
  #env.Alias("install",env.Install(EXAMPLEINSTALLDIR,[elf,hex,bin,lst,size]))
